//
//  MCDiscountPayViewController.swift
//  StoreManage
//
//  Created by MC on 2017/12/19.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCDiscountPayViewController: MCViewController {

    private var pageNumber = 1
    public var type = 0        // 线上 type=0  线下type=1
    
    private var discount = 0
    private var discountRate = 0

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        sendNetworking()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func offlineButtonClicked() {
        type = 1
        pageNumber = 1
        sectionHeadView.offlineButton.isSelected = true
        sectionHeadView.onlineButton.isSelected = false
        sendNetworking()
    }
    
    @objc func onlineButtonClicked() {
        type = 0
        pageNumber = 1
        sectionHeadView.offlineButton.isSelected = false
        sectionHeadView.onlineButton.isSelected = true
        
        sendNetworking()
    }
    
    @objc func rightItemClicked() {
        let vc = MCDiscountSettingViewController()
        vc.discount = discount
        vc.discountRate = discountRate
        
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }

    // MARK: - Network
    func sendNetworking() {
        let para = [
            "pageNum"  : pageNumber,
            "pageSize" : "10",
            "type"     : type
            ] as [String : Any]
        
        
        MCNetwork_post(url: JAVA_orderPayBaSelectByPage, para: para as [String : AnyObject], vc: self,hudSuperView: self.view,isHiddenTabbar: true, success: { (response) in
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()

            if self.pageNumber == 1 {
                self.dataArrayM.removeAllObjects()
            }
            
            let dataDict = response["data"] as! [String : AnyObject]
            
            let totalFee = dataDict["totalFee"] as! NSNumber
            let totalFee1 = dataDict["totalFee1"] as! NSNumber
            let yestodayTotalFee = dataDict["yestodayTotalFee"] as! NSNumber
            let yestodayTotalFee1 = dataDict["yestodayTotalFee1"] as! NSNumber
            let discount = dataDict["discount"] as! NSNumber           // 线上买单
            let discountRate = dataDict["discountRate"] as! NSNumber   // 线下扫码
            let list = dataDict["list"] as! NSArray
            
            self.discount = discount.intValue
            self.discountRate = discountRate.intValue
            
            let yes_1 = "昨日: " + MCString_judgemantDecimalsIsZero(figure: yestodayTotalFee.intValue)
            self.headView.yesterdayView.yesterdayLabel.attributedText = yes_1.MCAttributedString_color(color: UIColor.MCMain, range: NSRange.init(location: 3, length: yes_1.count-3))
            
            let yes_2 =  "总计: " + MCString_judgemantDecimalsIsZero(figure: totalFee.intValue)
            self.headView.yesterdayView.totalLabel.attributedText = yes_2.MCAttributedString_color(color: UIColor.MCMain, range: NSRange.init(location: 3, length: yes_2.count-3))
            
            let tol_1 = "昨日: " + MCString_judgemantDecimalsIsZero(figure: yestodayTotalFee1.intValue)
            self.headView.totalView.yesterdayLabel.attributedText = tol_1.MCAttributedString_color(color: UIColor.MCMain, range: NSRange.init(location: 3, length: tol_1.count-3))
            
            
            let tol_2 = "总计: " + MCString_judgemantDecimalsIsZero(figure: totalFee1.intValue)
            
            self.headView.totalView.totalLabel.attributedText = tol_2.MCAttributedString_color(color: UIColor.MCMain, range: NSRange.init(location: 3, length: tol_2.count-3))
            
            if list.count + self.dataArrayM.count == 0 {
                self.tableView.showBgImage()
            } else {
                self.tableView.hideBgImage()
                
                for dict in list {
                    let model = MCDiscountPayModel()
                    model.setValuesForKeys(dict as! [String : Any])
                    self.dataArrayM.add(model)
                }
            }
            
            self.tableView.reloadData()


        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "买单列表"
        self.view.backgroundColor = UIColor.white
        tableView.tableHeaderView = headView
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_text("优惠设置", target: self, selector: #selector(rightItemClicked))

        
        
        self.tableView.mj_header = MCRefreshHeader.init(refreshingBlock: {
            self.pageNumber = 1
            self.sendNetworking()
        })
        
        self.tableView.mj_footer = MCRefreshFooter.init(refreshingBlock: {
            self.pageNumber += 1
            self.sendNetworking()
        })
    }
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }
    }
    
    // MARK: - Setter & Getter
    lazy private var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
    lazy var headView: MCDiscountPayHeadView = {
        let view = MCDiscountPayHeadView()
        return view
    }()
    
    lazy var sectionHeadView: MCDiscountPaySectionHeadView = {
        let view = MCDiscountPaySectionHeadView()
        view.onlineButton.addTarget(self, action: #selector(onlineButtonClicked), for: UIControlEvents.touchUpInside)
        view.offlineButton.addTarget(self, action: #selector(offlineButtonClicked), for: UIControlEvents.touchUpInside)
        return view
    }()
}

extension MCDiscountPayViewController : UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArrayM.count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        if type == 0 {
            sectionHeadView.onlineButton.isSelected = true
            sectionHeadView.offlineButton.isSelected = false
        } else {
            sectionHeadView.onlineButton.isSelected = false
            sectionHeadView.offlineButton.isSelected = true
        }
        
        return sectionHeadView
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 327
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCDiscountPayCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.model = self.dataArrayM[indexPath.row] as! MCDiscountPayModel
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }
}

